<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="ranges属性的应用？若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV06-10-设备树-05-ranges属性">
<meta property="og:url" content="https://sumumm.github.io/post/238c1e95.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="ranges属性的应用？若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-10-%E8%AE%BE%E5%A4%87%E6%A0%91-05-ranges%E5%B1%9E%E6%80%A7/img/image-20250316104023581.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-10-%E8%AE%BE%E5%A4%87%E6%A0%91-05-ranges%E5%B1%9E%E6%80%A7/img/image-20250316104118501.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-10-%E8%AE%BE%E5%A4%87%E6%A0%91-05-ranges%E5%B1%9E%E6%80%A7/img/image-20250310194204335.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-10-%E8%AE%BE%E5%A4%87%E6%A0%91-05-ranges%E5%B1%9E%E6%80%A7/img/480488-20170820191851725-1533846276.png">
<meta property="article:published_time" content="2025-03-16T23:45:55.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.055Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV06-驱动开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-10-%E8%AE%BE%E5%A4%87%E6%A0%91-05-ranges%E5%B1%9E%E6%80%A7/img/image-20250316104023581.png">


<link rel="canonical" href="https://sumumm.github.io/post/238c1e95.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":"","permalink":"https://sumumm.github.io/post/238c1e95.html","path":"post/238c1e95.html","title":"LV06-10-设备树-05-ranges属性"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV06-10-设备树-05-ranges属性 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81platform-device%E8%B5%84%E6%BA%90%E8%8E%B7%E5%8F%96"><span class="nav-text">一、platform_device资源获取</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%80%8E%E4%B9%88%E8%8E%B7%E5%8F%96%E8%B5%84%E6%BA%90%EF%BC%9F"><span class="nav-text">1. 怎么获取资源？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-platform-get-resource"><span class="nav-text">2. platform_get_resource()</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%B8%80%E4%B8%AA%E6%8A%A5%E9%94%99"><span class="nav-text">3. 一个报错</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E8%AE%BE%E5%A4%87%E6%A0%91"><span class="nav-text">3.1 设备树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-demo%E6%BA%90%E7%A0%81"><span class="nav-text">3.2 demo源码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-%E5%BC%80%E5%8F%91%E6%9D%BF%E6%B5%8B%E8%AF%95"><span class="nav-text">3.3 开发板测试</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E8%8E%B7%E5%8F%96%E8%B5%84%E6%BA%90%E5%A4%B1%E8%B4%A5%E5%88%86%E6%9E%90"><span class="nav-text">4. 获取资源失败分析</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E8%BF%94%E5%9B%9E%E9%94%99%E8%AF%AF%E5%80%BC%E5%88%86%E6%9E%90"><span class="nav-text">4.1 返回错误值分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-of-platform-device-create-pdata"><span class="nav-text">4.2 of_platform_device_create_pdata()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3-of-device-alloc"><span class="nav-text">4.3 of_device_alloc()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4-of-translate-address"><span class="nav-text">4.4 __of_translate_address() </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-5-of-translate-one"><span class="nav-text">4.5 of_translate_one()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-6-%E4%B8%80%E6%AD%A5%E4%B8%80%E6%AD%A5%E8%BF%94%E5%9B%9E"><span class="nav-text">4.6 一步一步返回</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-7-%E6%80%BB%E7%BB%93"><span class="nav-text">4.7 总结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3"><span class="nav-text">5. 问题解决</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-%E8%AE%BE%E5%A4%87%E6%A0%91"><span class="nav-text">5.1 设备树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E5%BC%80%E5%8F%91%E6%9D%BF%E6%B5%8B%E8%AF%95"><span class="nav-text">5.2 开发板测试</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81ranges%E5%B1%9E%E6%80%A7%EF%BC%9F"><span class="nav-text">二、ranges属性？</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-ranges-%E5%B1%9E%E6%80%A7%E4%BB%8B%E7%BB%8D"><span class="nav-text">1. ranges 属性介绍  </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%B8%BE%E4%B8%AA%E4%BE%8B%E5%AD%90"><span class="nav-text">2. 举个例子</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%AE%9E%E4%BE%8B1"><span class="nav-text">2.1 实例1</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-1-%E8%AE%BE%E5%A4%87%E6%A0%91%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.1.1 设备树实例</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-2-%E9%A9%B1%E5%8A%A8demo"><span class="nav-text">2.1.2 驱动demo</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-3-%E5%BC%80%E5%8F%91%E6%9D%BF%E6%B5%8B%E8%AF%95"><span class="nav-text">2.1.3 开发板测试</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-4-%E6%80%BB%E7%BB%93"><span class="nav-text">2.1.4 总结</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E5%AE%9E%E4%BE%8B2"><span class="nav-text">2.2 实例2</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E6%80%BB%E7%BB%93"><span class="nav-text">2.3 总结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E8%AE%BE%E5%A4%87%E5%88%86%E7%B1%BB"><span class="nav-text">3. 设备分类  </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E5%86%85%E5%AD%98%E6%98%A0%E5%B0%84%E5%9E%8B%E8%AE%BE%E5%A4%87"><span class="nav-text">3.1 内存映射型设备  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E9%9D%9E%E5%86%85%E5%AD%98%E6%98%A0%E5%B0%84%E5%9E%8B%E8%AE%BE%E5%A4%87"><span class="nav-text">3.2 非内存映射型设备  </span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E6%98%A0%E5%B0%84%E5%9C%B0%E5%9D%80%E8%AE%A1%E7%AE%97"><span class="nav-text">4. 映射地址计算  </span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/238c1e95.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV06-10-设备树-05-ranges属性 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV06-10-设备树-05-ranges属性
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2025-03-17 07:45:55" itemprop="dateCreated datePublished" datetime="2025-03-17T07:45:55+08:00">2025-03-17</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/" itemprop="url" rel="index"><span itemprop="name">02IMX6ULL平台</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV06-驱动开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>6.4k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>23 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>ranges属性的应用？若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-10-%E8%AE%BE%E5%A4%87%E6%A0%91-05-ranges%E5%B1%9E%E6%80%A7/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" rowspan="5">PC端开发环境</td>        <td align="center" width=150px>Windows</td>        <td align="left">Windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu20.04.2的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 17 Pro</td>        <td align="left">17.6.0 build-24238078</td>      </tr>    <tr>        <td align="center">终端软件</td>        <td align="left">MobaXterm(Professional Edition v23.0 Build 5042 (license))</td>    </tr>    <tr>        <td align="center">Win32DiskImager</td>        <td align="left">Win32DiskImager v1.0</td>      </tr>    <tr>        <td align="center" rowspan="3">Linux开发板环境</td>        <td align="center">Linux开发板</td>        <td align="left">正点原子 i.MX6ULL Linux 阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，使用的uboot版本为U-Boot 2019.04</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.19.71(NXP官方提供)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="5">官方网站</td>        <td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td>        <td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/" target="_blank">https://www.nxp.com.cn/ </a></td>        <td align="left">NXP官方网站</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxpic.org.cn/" target="_blank">https://www.nxpic.org.cn/</a></td><td align="left">NXP 官方社区</td>    </tr>    <tr>        <td align="left"><a href="https://u-boot.readthedocs.io/en/latest/" target="_blank">https://u-boot.readthedocs.io/en/latest/</a></td><td align="left">u-boot官网</td>    </tr>    <tr>        <td align="left"><a href="https://www.kernel.org/" target="_blank">https://www.kernel.org/</a></td><td align="left">linux内核官网</td>    </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="3">NXP</td>        <td align="left"><a href="https://github.com/nxp-imx" target="_blank">https://github.com/nxp-imx</a></td>        <td align="left">NXP imx开发资源GitHub组织，里边会有u-boot和linux内核的仓库</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/linux-imx/releases/tag/v4.19.71" target="_blank">nxp-imx/linux-imx/releases/tag/v4.19.71</a></td>        <td align="left">NXP linux内核仓库tags中的v4.19.71</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/uboot-imx/releases/tag/rel_imx_4.19.35_1.1.0" target="_blank">nxp-imx/uboot-imx/releases/tag/rel_imx_4.19.35_1.1.0</a></td>        <td align="left">NXP u-boot仓库tags中的rel_imx_4.19.35_1.1.0</td>    </tr>    <tr>        <td align="center" rowspan="2">I.MX6ULL</td>        <td align="left"><a href="https://www.nxp.com.cn/docs/en/data-sheet/IMX6ULLIEC.pdf" target="_blank">i.MX 6ULL Applications Processors for Industrial Products</a></td>        <td align="left">I.MX6ULL 芯片手册（datasheet，可以在线查看）</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/webapp/Download?colCode=IMX6ULLRM&lang_cd=zh" target="_blank">i.MX 6ULL Applications ProcessorReference Manual</a></td>        <td align="left">I.MX6ULL 参考手册（下载后才能查看，需要登录NXP官网）</td>    </tr>    <tr>        <td align="center" rowspan="3">Source Code</td>        <td align="left"><a href="https://elixir.bootlin.com/linux/latest/source" target="_blank">https://elixir.bootlin.com/linux/latest/source</a></td>        <td align="left">linux kernel源码</td>    </tr>    <tr>        <td align="left"><a href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4" target="_blank">kernel/git/stable/linux.git - Linux kernel stable tree</a></td>        <td align="left">linux kernel源码(官网,tag 4.19.71)</td>    </tr>    <tr>        <td align="left"><a href="https://elixir.bootlin.com/u-boot/latest/source" target="_blank">https://elixir.bootlin.com/u-boot/latest/source</a></td>        <td align="left">uboot源码</td>    </tr></table>
              </div>
            </details>

<h1 id="一、platform-device资源获取"><a href="#一、platform-device资源获取" class="headerlink" title="一、platform_device资源获取"></a><font size=3>一、platform_device资源获取</font></h1><h2 id="1-怎么获取资源？"><a href="#1-怎么获取资源？" class="headerlink" title="1. 怎么获取资源？"></a><font size=3>1. 怎么获取资源？</font></h2><p>前面我们知道，对于platform_device来说，我们有两种方式可以获取对应的节点，一是of操作函数，就正常的查找对应节点即可，二是 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/include/linux/platform_device.h#L27">platform_device</a>.<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/include/linux/device.h#L1029">dev</a>.<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/include/linux/of.h#L51">of_node</a>。通过这两种方式我们可以获取到节点的地址，从而获取节点中各种资源。</p>
<p>由于设备树在系统启动的时候有部分节点会转化为 platform 设备，对于platform_device设备来说</p>
<p>（1）如果在设备树节点里使用 reg 属性，那么内核生成对应的 platform_device时会用 reg 属性来设置 IORESOURCE_MEM 类型的资源。</p>
<p>（2）如果在设备树节点里使用 interrupts 属性 ，那么内核生成对应的platform_device 时会用 reg 属性来设置 IORESOURCE_IRQ 类型的资源。（对于interrupts 属性，内核会检查它的有效性，所以不建议在设备树里使用该属性来表示其他资源。 ）</p>
<p>所以，实际上当设备树中的节点被转换为platform_device 后，设备树中的 reg 属性、 interrupts 属性也会被转换为“ resource”，内核为我们提供了函数<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/base/platform.c#L60">platform_get_resource()</a>来获取这些不同的资源。接下来就来看一下吧。</p>
<h2 id="2-platform-get-resource"><a href="#2-platform-get-resource" class="headerlink" title="2. platform_get_resource()"></a><font size=3>2. <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/base/platform.c#L60">platform_get_resource()</a></font></h2><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/base/platform.c#L60">platform_get_resource()</a>函数定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> resource *<span class="title function_">platform_get_resource</span><span class="params">(<span class="keyword">struct</span> platform_device *dev,</span></span><br><span class="line"><span class="params">				       <span class="type">unsigned</span> <span class="type">int</span> type, <span class="type">unsigned</span> <span class="type">int</span> num)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; dev-&gt;num_resources; i++) &#123;</span><br><span class="line">		<span class="class"><span class="keyword">struct</span> <span class="title">resource</span> *<span class="title">r</span> =</span> &amp;dev-&gt;resource[i];</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (type == resource_type(r) &amp;&amp; num-- == <span class="number">0</span>)</span><br><span class="line">			<span class="keyword">return</span> r;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">&#125;</span><br><span class="line">EXPORT_SYMBOL_GPL(platform_get_resource);</span><br></pre></td></tr></table></figure>

<p><strong>参数说明：</strong></p>
<ul>
<li>dev：struct platform_device类型指针，包含了设备树节点的相关信息。</li>
<li>type：资源的类型，IORESOURCE_MEM、 IORESOURCE_REG、IORESOURCE_IRQ 等 </li>
<li>num：这类资源中的哪一个。</li>
</ul>
<h2 id="3-一个报错"><a href="#3-一个报错" class="headerlink" title="3. 一个报错"></a><font size=3>3. 一个报错</font></h2><p>那么我们用上面的<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/base/platform.c#L60">platform_get_resource()</a>函数来尝试获取一下资源。</p>
<h3 id="3-1-设备树"><a href="#3-1-设备树" class="headerlink" title="3.1 设备树"></a><font size=3>3.1 设备树</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">/dts-v1/;</span><br><span class="line"></span><br><span class="line">/ &#123;</span><br><span class="line">	model = <span class="string">&quot;Freescale i.MX6 UlltraLite ALPHA EMMC Board&quot;</span>;</span><br><span class="line">	compatible = <span class="string">&quot;fsl,imx6ull-alpha-emmc&quot;</span>, <span class="string">&quot;fsl,imx6ull&quot;</span>;</span><br><span class="line"></span><br><span class="line">    alpha &#123;</span><br><span class="line">        <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">        <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">        compatible = <span class="string">&quot;simple-bus&quot;</span>;</span><br><span class="line"></span><br><span class="line">        sdev_led &#123;</span><br><span class="line">            <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">            <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">            compatible = <span class="string">&quot;led&quot;</span>;</span><br><span class="line">            status = <span class="string">&quot;okay&quot;</span>;</span><br><span class="line">            reg = &lt; <span class="number">0X020C406C</span> <span class="number">0X04</span> <span class="comment">/* CCM_CCGR1_BASE */</span></span><br><span class="line">                    <span class="number">0X020E0068</span> <span class="number">0X04</span> <span class="comment">/* SW_MUX_GPIO1_IO03_BASE */</span></span><br><span class="line">                    <span class="number">0X020E02F4</span> <span class="number">0X04</span> <span class="comment">/* SW_PAD_GPIO1_IO03_BASE */</span></span><br><span class="line">                    <span class="number">0X0209C000</span> <span class="number">0X04</span> <span class="comment">/* GPIO1_DR_BASE */</span></span><br><span class="line">                    <span class="number">0X0209C004</span> <span class="number">0X04</span> &gt;; <span class="comment">/* GPIO1_GDIR_BASE */</span></span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="3-2-demo源码"><a href="#3-2-demo源码" class="headerlink" title="3.2 demo源码"></a><font size=3>3.2 demo源码</font></h3><p>源码可以看这里：<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-driver-demo/tree/master/11_device_tree/06_ranges_property">11_device_tree&#x2F;06_ranges_property · 苏木&#x2F;imx6ull-driver-demo - 码云 - 开源中国</a>。主要是添加一个<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/base/platform.c#L60">platform_get_resource()</a>函数获取资源的部分：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">sdrv_probe</span><span class="params">(<span class="keyword">struct</span> platform_device *pdev)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">resource</span> *<span class="title">p_resources</span>;</span></span><br><span class="line">    PRT(<span class="string">&quot;probing platform device &amp; driver!pdev-&gt;name=%s\n&quot;</span>, pdev-&gt;name);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取平台设备的资源</span></span><br><span class="line">    p_resources = platform_get_resource(pdev, IORESOURCE_MEM, <span class="number">0</span>);</span><br><span class="line">    <span class="keyword">if</span> (p_resources == <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 如果获取资源失败， 打印 value_compatible 的值</span></span><br><span class="line">        PRTE(<span class="string">&quot;platform_get_resource is error\n&quot;</span>);</span><br><span class="line">        ret = <span class="number">-1</span>;</span><br><span class="line">        <span class="keyword">goto</span> err_platform_get_resource;</span><br><span class="line">    &#125;</span><br><span class="line">    PRT(<span class="string">&quot;%s start: 0x%x, end: 0x%x\n&quot;</span>,</span><br><span class="line">        p_resources-&gt;name, p_resources-&gt;start, p_resources-&gt;end + <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">err_platform_get_resource:</span><br><span class="line">    <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-3-开发板测试"><a href="#3-3-开发板测试" class="headerlink" title="3.3 开发板测试"></a><font size=3>3.3 开发板测试</font></h3><p>我们编译后，加载对应的模块，出现下面的报错：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-10-%E8%AE%BE%E5%A4%87%E6%A0%91-05-ranges%E5%B1%9E%E6%80%A7/img/image-20250316104023581.png" alt="image-20250316104023581" />

<p>可以看到使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/base/platform.c#L60">platform_get_resource()</a> 函数获取 reg 资源的函数失败了，后面我们就来分析一下为什么。</p>
<h2 id="4-获取资源失败分析"><a href="#4-获取资源失败分析" class="headerlink" title="4. 获取资源失败分析"></a><font size=3>4. 获取资源失败分析</font></h2><h3 id="4-1-返回错误值分析"><a href="#4-1-返回错误值分析" class="headerlink" title="4.1 返回错误值分析"></a><font size=3>4.1 返回错误值分析</font></h3><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/base/platform.c#L60">platform_get_resource()</a>定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> resource *<span class="title function_">platform_get_resource</span><span class="params">(<span class="keyword">struct</span> platform_device *dev,</span></span><br><span class="line"><span class="params">				       <span class="type">unsigned</span> <span class="type">int</span> type, <span class="type">unsigned</span> <span class="type">int</span> num)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; dev-&gt;num_resources; i++) &#123;</span><br><span class="line">		<span class="class"><span class="keyword">struct</span> <span class="title">resource</span> *<span class="title">r</span> =</span> &amp;dev-&gt;resource[i];</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (type == resource_type(r) &amp;&amp; num-- == <span class="number">0</span>)</span><br><span class="line">			<span class="keyword">return</span> r;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>该函数返回 NULL 符合上面报错的时候的情况， 这里返回 NULL 的情况有两种可能性， 一种是没进入上面的 for 循环直接返回了 NULL， 另外一种是进入了 for 循环， 但是类型匹配不正确， 跳出 for循环之后再返回 NULL。   </p>
<p>这里的类型一定是匹配的， 所以我们就来寻找为什么没有进入 for 循环，这里只有一种可能， 也就是 dev-&gt;num_resources 为 0。所以现在的目标来到了寻找 dev-&gt;num_resources 是在哪里进行的赋值， 前面已经知道过了由设备树转换为 platform 的过程， 而且在系统启动后， 在对应目录下也有了相应的节点：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ls /sys/bus/platform/devices/</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-10-%E8%AE%BE%E5%A4%87%E6%A0%91-05-ranges%E5%B1%9E%E6%80%A7/img/image-20250316104118501.png" alt="image-20250316104118501" />

<p>证明转换是没问题的， 所以继续寻找中间转换过程中有关资源数量的相关函数， 定位到 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/platform.c#L162">of_platform_device_create_pdata()</a>：</p>
<h3 id="4-2-of-platform-device-create-pdata"><a href="#4-2-of-platform-device-create-pdata" class="headerlink" title="4.2 of_platform_device_create_pdata()"></a><font size=3>4.2 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/platform.c#L162">of_platform_device_create_pdata()</a></font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="keyword">struct</span> platform_device *<span class="title function_">of_platform_device_create_pdata</span><span class="params">(</span></span><br><span class="line"><span class="params">					<span class="keyword">struct</span> device_node *np,</span></span><br><span class="line"><span class="params">					<span class="type">const</span> <span class="type">char</span> *bus_id,</span></span><br><span class="line"><span class="params">					<span class="type">void</span> *platform_data,</span></span><br><span class="line"><span class="params">					<span class="keyword">struct</span> device *parent)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">platform_device</span> *<span class="title">dev</span>;</span></span><br><span class="line">	<span class="comment">/* 检查设备节点是否可用或已填充 */</span></span><br><span class="line">	<span class="keyword">if</span> (!of_device_is_available(np) ||</span><br><span class="line">	    of_node_test_and_set_flag(np, OF_POPULATED))</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">	<span class="comment">/* 分配平台设备结构体 */</span></span><br><span class="line">	dev = of_device_alloc(np, bus_id, parent);</span><br><span class="line">	<span class="keyword">if</span> (!dev)</span><br><span class="line">		<span class="keyword">goto</span> err_clear_flag;</span><br><span class="line">	<span class="comment">/* 设置平台设备的一些属性 */</span></span><br><span class="line">	dev-&gt;dev.coherent_dma_mask = DMA_BIT_MASK(<span class="number">32</span>);</span><br><span class="line">	<span class="keyword">if</span> (!dev-&gt;dev.dma_mask)</span><br><span class="line">		dev-&gt;dev.dma_mask = &amp;dev-&gt;dev.coherent_dma_mask;</span><br><span class="line">	dev-&gt;dev.bus = &amp;platform_bus_type;</span><br><span class="line">	dev-&gt;dev.platform_data = platform_data;</span><br><span class="line">	of_msi_configure(&amp;dev-&gt;dev, dev-&gt;dev.of_node);</span><br><span class="line">	<span class="comment">/* 将平台设备添加到设备模型中 */</span></span><br><span class="line">	<span class="keyword">if</span> (of_device_add(dev) != <span class="number">0</span>) &#123;</span><br><span class="line">		platform_device_put(dev);</span><br><span class="line">		<span class="keyword">goto</span> err_clear_flag;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> dev;</span><br><span class="line"></span><br><span class="line">err_clear_flag:</span><br><span class="line">    <span class="comment">/* 清除设备节点的已填充标志 */</span></span><br><span class="line">	of_node_clear_flag(np, OF_POPULATED);</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>第13行：函数调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/platform.c#L107">of_device_alloc()</a>分配一个平台设备结构体， 并将设备节点指针、 设备标识符和父设备指针传递给它， 正是该函数决定的 resource.num。</p>
<h3 id="4-3-of-device-alloc"><a href="#4-3-of-device-alloc" class="headerlink" title="4.3 of_device_alloc()"></a><font size=3>4.3 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/platform.c#L107">of_device_alloc()</a></font></h3><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/platform.c#L107">of_device_alloc()</a>函数定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> platform_device *<span class="title function_">of_device_alloc</span><span class="params">(<span class="keyword">struct</span> device_node *np,</span></span><br><span class="line"><span class="params">				  <span class="type">const</span> <span class="type">char</span> *bus_id,</span></span><br><span class="line"><span class="params">				  <span class="keyword">struct</span> device *parent)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">platform_device</span> *<span class="title">dev</span>;</span></span><br><span class="line">	<span class="type">int</span> rc, i, num_reg = <span class="number">0</span>, num_irq;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">resource</span> *<span class="title">res</span>, <span class="title">temp_res</span>;</span></span><br><span class="line"></span><br><span class="line">	dev = platform_device_alloc(<span class="string">&quot;&quot;</span>, PLATFORM_DEVID_NONE);</span><br><span class="line">	<span class="keyword">if</span> (!dev)</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* count the io and irq resources */</span></span><br><span class="line">	<span class="keyword">while</span> (of_address_to_resource(np, num_reg, &amp;temp_res) == <span class="number">0</span>)</span><br><span class="line">		num_reg++;</span><br><span class="line">	num_irq = of_irq_count(np);</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* Populate the resource table */</span></span><br><span class="line">	<span class="keyword">if</span> (num_irq || num_reg) &#123;</span><br><span class="line">		res = kcalloc(num_irq + num_reg, <span class="keyword">sizeof</span>(*res), GFP_KERNEL);</span><br><span class="line">		<span class="keyword">if</span> (!res) &#123;</span><br><span class="line">			platform_device_put(dev);</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		dev-&gt;num_resources = num_reg + num_irq;</span><br><span class="line">		dev-&gt;resource = res;</span><br><span class="line">		<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; num_reg; i++, res++) &#123;</span><br><span class="line">			rc = of_address_to_resource(np, i, res);</span><br><span class="line">			WARN_ON(rc);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (of_irq_to_resource_table(np, res, num_irq) != num_irq)</span><br><span class="line">			pr_debug(<span class="string">&quot;not all legacy IRQ resources mapped for %pOFn\n&quot;</span>,</span><br><span class="line">				 np);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	dev-&gt;dev.of_node = of_node_get(np);</span><br><span class="line">	dev-&gt;dev.fwnode = &amp;np-&gt;fwnode;</span><br><span class="line">	dev-&gt;dev.parent = parent ? : &amp;platform_bus;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (bus_id)</span><br><span class="line">		dev_set_name(&amp;dev-&gt;dev, <span class="string">&quot;%s&quot;</span>, bus_id);</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		of_device_make_bus_id(&amp;dev-&gt;dev);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> dev;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在第 28 行出现了 for 循环的 dev-&gt;num_resources &#x3D; num_reg + num_irq; reg 的 number 和 irq的 number， 由于在设备树中并没有添加中断相关的属性 num_irq 为 0， 那这里的 num_reg 是哪里确定的呢。我们向上找到 14、 15 行， 具体内容如下所示：  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* count the io and irq resources */</span></span><br><span class="line"><span class="keyword">while</span> (of_address_to_resource(np, num_reg, &amp;temp_res) == <span class="number">0</span>)</span><br><span class="line">	num_reg++;</span><br><span class="line">num_irq = of_irq_count(np);</span><br></pre></td></tr></table></figure>

<p>所以这里我们需要去看一下  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L780">of_address_to_resource()</a> 函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">of_address_to_resource</span><span class="params">(<span class="keyword">struct</span> device_node *dev, <span class="type">int</span> index,</span></span><br><span class="line"><span class="params">			   <span class="keyword">struct</span> resource *r)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">const</span> __be32	*addrp;</span><br><span class="line">	u64		size;</span><br><span class="line">	<span class="type">unsigned</span> <span class="type">int</span>	flags;</span><br><span class="line">	<span class="type">const</span> <span class="type">char</span>	*name = <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">	addrp = of_get_address(dev, index, &amp;size, &amp;flags);</span><br><span class="line">	<span class="keyword">if</span> (addrp == <span class="literal">NULL</span>)</span><br><span class="line">		<span class="keyword">return</span> -EINVAL;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* Get optional &quot;reg-names&quot; property to add a name to a resource */</span></span><br><span class="line">	of_property_read_string_index(dev, <span class="string">&quot;reg-names&quot;</span>,	index, &amp;name);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> __of_address_to_resource(dev, addrp, size, flags, name, r);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>第 9 行：获取 reg 属性的地址、 大小和类型， 在设备树中 reg 属性已经存在了， 所以这里会正确返回。</p>
<p>第 14 行： 读取 reg-names 属性， 由于设备树中没有定义这个属性， 所以该函数不会有影响.</p>
<p>第 16 行：最后具有决定性作用的函数就是返回的 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L755">__of_address_to_resource()</a> 函数 :</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">int</span> __of_address_to_resource(<span class="keyword">struct</span> device_node *dev,</span><br><span class="line">		<span class="type">const</span> __be32 *addrp, u64 size, <span class="type">unsigned</span> <span class="type">int</span> flags,</span><br><span class="line">		<span class="type">const</span> <span class="type">char</span> *name, <span class="keyword">struct</span> resource *r)</span><br><span class="line">&#123;</span><br><span class="line">	u64 taddr;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (flags &amp; IORESOURCE_MEM)</span><br><span class="line">		taddr = of_translate_address(dev, addrp);</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (flags &amp; IORESOURCE_IO)</span><br><span class="line">		taddr = of_translate_ioport(dev, addrp, size);</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">return</span> -EINVAL;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (taddr == OF_BAD_ADDR)</span><br><span class="line">		<span class="keyword">return</span> -EINVAL;</span><br><span class="line">	<span class="built_in">memset</span>(r, <span class="number">0</span>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> resource));</span><br><span class="line"></span><br><span class="line">	r-&gt;start = taddr;</span><br><span class="line">	r-&gt;end = taddr + size - <span class="number">1</span>;</span><br><span class="line">	r-&gt;flags = flags;</span><br><span class="line">	r-&gt;name = name ? name : dev-&gt;full_name;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>reg 属性的 flags 为 IORESOURCE_MEM， 所以又会执行第 7 行的 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L663">of_translate_address()</a> 函数，看一下这个函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">u64 <span class="title function_">of_translate_address</span><span class="params">(<span class="keyword">struct</span> device_node *dev, <span class="type">const</span> __be32 *in_addr)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">device_node</span> *<span class="title">host</span>;</span></span><br><span class="line">	u64 ret;</span><br><span class="line"></span><br><span class="line">	ret = __of_translate_address(dev, in_addr, <span class="string">&quot;ranges&quot;</span>, &amp;host);</span><br><span class="line">	<span class="keyword">if</span> (host) &#123;</span><br><span class="line">		of_node_put(host);</span><br><span class="line">		<span class="keyword">return</span> OF_BAD_ADDR;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>该函数的重点在第 6 行， 上述函数实际上是 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L571">__of_translate_address()</a> 函数的封装， 其中传入的第三个参数“ranges”是我们要关注的重点.</p>
<h3 id="4-4-of-translate-address"><a href="#4-4-of-translate-address" class="headerlink" title="4.4 __of_translate_address() "></a><font size=3>4.4 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L571">__of_translate_address()</a> </font></h3><p>继续跳转到<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L571">__of_translate_address()</a> 函数的定义:</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> u64 __of_translate_address(<span class="keyword">struct</span> device_node *dev,</span><br><span class="line">				  <span class="type">const</span> __be32 *in_addr, <span class="type">const</span> <span class="type">char</span> *rprop,</span><br><span class="line">				  <span class="keyword">struct</span> device_node **host)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">//函数有点长，直接看在线的吧。</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L588">588</a> - <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L591">591</a> 行：获取父节点和匹配的总线类型</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Get parent &amp; match bus type */</span></span><br><span class="line">parent = of_get_parent(dev);</span><br><span class="line"><span class="keyword">if</span> (parent == <span class="literal">NULL</span>)</span><br><span class="line">	<span class="keyword">goto</span> bail;</span><br><span class="line">bus = of_match_bus(parent);</span><br></pre></td></tr></table></figure>

<ul>
<li><p>第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L593">593</a> - <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L599">599</a> 行获取 address-cell 和 size-cells  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Count address cells &amp; copy address locally */</span></span><br><span class="line">bus-&gt;count_cells(dev, &amp;na, &amp;ns);</span><br><span class="line"><span class="keyword">if</span> (!OF_CHECK_COUNTS(na, ns)) &#123;</span><br><span class="line">	pr_debug(<span class="string">&quot;Bad cell count for %pOF\n&quot;</span>, dev);</span><br><span class="line">	<span class="keyword">goto</span> bail;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">memcpy</span>(addr, in_addr, na * <span class="number">4</span>);</span><br></pre></td></tr></table></figure>
</li>
<li><p>第 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L646">646</a> 行</p>
</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Translate */</span></span><br><span class="line"><span class="keyword">for</span> (;;) &#123;</span><br><span class="line">	<span class="comment">//......</span></span><br><span class="line">	<span class="comment">/* Apply bus translation */</span></span><br><span class="line">	<span class="keyword">if</span> (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* Complete the move up one level */</span></span><br><span class="line">	na = pna;</span><br><span class="line">	ns = pns;</span><br><span class="line">	bus = pbus;</span><br><span class="line"></span><br><span class="line">	of_dump_addr(<span class="string">&quot;one level translation:&quot;</span>, addr, na);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L496">of_translate_one()</a> 函数进行转换， 其中 rprop 参数表示要转换的资源属性， 该参数的值为传入的“ranges”  </p>
<h3 id="4-5-of-translate-one"><a href="#4-5-of-translate-one" class="headerlink" title="4.5 of_translate_one()"></a><font size=3>4.5 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L496">of_translate_one()</a></font></h3><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L496">of_translate_one()</a>函数定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">of_translate_one</span><span class="params">(<span class="keyword">struct</span> device_node *parent, <span class="keyword">struct</span> of_bus *bus,</span></span><br><span class="line"><span class="params">			    <span class="keyword">struct</span> of_bus *pbus, __be32 *addr,</span></span><br><span class="line"><span class="params">			    <span class="type">int</span> na, <span class="type">int</span> ns, <span class="type">int</span> pna, <span class="type">const</span> <span class="type">char</span> *rprop)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">const</span> __be32 *ranges;</span><br><span class="line">	<span class="type">unsigned</span> <span class="type">int</span> rlen;</span><br><span class="line">	<span class="type">int</span> rone;</span><br><span class="line">	u64 offset = OF_BAD_ADDR;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	 * Normally, an absence of a &quot;ranges&quot; property means we are</span></span><br><span class="line"><span class="comment">	 * crossing a non-translatable boundary, and thus the addresses</span></span><br><span class="line"><span class="comment">	 * below the current cannot be converted to CPU physical ones.</span></span><br><span class="line"><span class="comment">	 * Unfortunately, while this is very clear in the spec, it&#x27;s not</span></span><br><span class="line"><span class="comment">	 * what Apple understood, and they do have things like /uni-n or</span></span><br><span class="line"><span class="comment">	 * /ht nodes with no &quot;ranges&quot; property and a lot of perfectly</span></span><br><span class="line"><span class="comment">	 * useable mapped devices below them. Thus we treat the absence of</span></span><br><span class="line"><span class="comment">	 * &quot;ranges&quot; as equivalent to an empty &quot;ranges&quot; property which means</span></span><br><span class="line"><span class="comment">	 * a 1:1 translation at that level. It&#x27;s up to the caller not to try</span></span><br><span class="line"><span class="comment">	 * to translate addresses that aren&#x27;t supposed to be translated in</span></span><br><span class="line"><span class="comment">	 * the first place. --BenH.</span></span><br><span class="line"><span class="comment">	 *</span></span><br><span class="line"><span class="comment">	 * As far as we know, this damage only exists on Apple machines, so</span></span><br><span class="line"><span class="comment">	 * This code is only enabled on powerpc. --gcl</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	ranges = of_get_property(parent, rprop, &amp;rlen);</span><br><span class="line">	<span class="keyword">if</span> (ranges == <span class="literal">NULL</span> &amp;&amp; !of_empty_ranges_quirk(parent)) &#123;</span><br><span class="line">		pr_debug(<span class="string">&quot;no ranges; cannot translate\n&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (ranges == <span class="literal">NULL</span> || rlen == <span class="number">0</span>) &#123;</span><br><span class="line">		offset = of_read_number(addr, na);</span><br><span class="line">		<span class="built_in">memset</span>(addr, <span class="number">0</span>, pna * <span class="number">4</span>);</span><br><span class="line">		pr_debug(<span class="string">&quot;empty ranges; 1:1 translation\n&quot;</span>);</span><br><span class="line">		<span class="keyword">goto</span> finish;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// ......</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在该函数的第 26 行使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/base.c#L335">of_get_property()</a> 函数获取“ranges”属性， 但由于在我们添加的设备树节点中并没有该属性， 所以这里的 ranges 值就为 NULL， 第 27 行的条件判断成立， 也就会返回 1。  </p>
<h3 id="4-6-一步一步返回"><a href="#4-6-一步一步返回" class="headerlink" title="4.6 一步一步返回"></a><font size=3>4.6 一步一步返回</font></h3><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/base.c#L335">of_get_property()</a> 函数返回NULL，所以 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L496">of_translate_one()</a> 返回1</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L571">__of_translate_address()</a> ：第<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L646">646</a>行</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">u64 result = OF_BAD_ADDR;</span><br><span class="line"><span class="comment">//......</span></span><br><span class="line"><span class="keyword">for</span> (;;) &#123;</span><br><span class="line">    <span class="comment">/* Apply bus translation */</span></span><br><span class="line">    <span class="keyword">if</span> (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里直接跳出循环，最终会返回 OF_BAD_ADDR 。再上一级的  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L780">of_address_to_resource()</a>  返回值也是 OF_ BAD _ADDR， 继续向上查找 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L755">__of_address_to_resource()</a> 函数会返回 EINVAL， <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/of/address.c#L780">of_address_to_resource()</a> 返回 EINVAL， 所以 num_reg 为 0;  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-10-%E8%AE%BE%E5%A4%87%E6%A0%91-05-ranges%E5%B1%9E%E6%80%A7/img/image-20250310194204335.png" alt="image-20250310194204335"  />

<h3 id="4-7-总结"><a href="#4-7-总结" class="headerlink" title="4.7 总结"></a><font size=3>4.7 总结</font></h3><p>到这里关于为什么 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.17/source/drivers/base/platform.c#L60">platform_get_resource()</a> 函数获取资源失败的问题就找到了， 只是因为在设备树中并没有这个名为 ranges 这个属性， 所以只需要对设备树进行 ranges 属性的添加即可。</p>
<h2 id="5-问题解决"><a href="#5-问题解决" class="headerlink" title="5. 问题解决"></a><font size=3>5. 问题解决</font></h2><h3 id="5-1-设备树"><a href="#5-1-设备树" class="headerlink" title="5.1 设备树"></a><font size=3>5.1 设备树</font></h3><p>修改设备树：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">/dts-v1/;</span><br><span class="line"></span><br><span class="line">/ &#123;</span><br><span class="line">	model = <span class="string">&quot;Freescale i.MX6 UlltraLite ALPHA EMMC Board&quot;</span>;</span><br><span class="line">	compatible = <span class="string">&quot;fsl,imx6ull-alpha-emmc&quot;</span>, <span class="string">&quot;fsl,imx6ull&quot;</span>;</span><br><span class="line"></span><br><span class="line">    alpha &#123;</span><br><span class="line">        <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">        <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">        ranges;</span><br><span class="line">        compatible = <span class="string">&quot;simple-bus&quot;</span>;</span><br><span class="line"></span><br><span class="line">        sdev_led &#123;</span><br><span class="line">            <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">            <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">            compatible = <span class="string">&quot;led&quot;</span>;</span><br><span class="line">            status = <span class="string">&quot;okay&quot;</span>;</span><br><span class="line">            reg = &lt; <span class="number">0X020C406C</span> <span class="number">0X04</span> <span class="comment">/* CCM_CCGR1_BASE */</span></span><br><span class="line">                    <span class="number">0X020E0068</span> <span class="number">0X04</span> <span class="comment">/* SW_MUX_GPIO1_IO03_BASE */</span></span><br><span class="line">                    <span class="number">0X020E02F4</span> <span class="number">0X04</span> <span class="comment">/* SW_PAD_GPIO1_IO03_BASE */</span></span><br><span class="line">                    <span class="number">0X0209C000</span> <span class="number">0X04</span> <span class="comment">/* GPIO1_DR_BASE */</span></span><br><span class="line">                    <span class="number">0X0209C004</span> <span class="number">0X04</span> &gt;; <span class="comment">/* GPIO1_GDIR_BASE */</span></span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="5-2-开发板测试"><a href="#5-2-开发板测试" class="headerlink" title="5.2 开发板测试"></a><font size=3>5.2 开发板测试</font></h3><p>我们更新开发板的设备树文件，然后重新加载驱动，就会发现，资源获取成功了：</p>
<h1 id="二、ranges属性？"><a href="#二、ranges属性？" class="headerlink" title="二、ranges属性？"></a><font size=3>二、ranges属性？</font></h1><p>虽然上面的问题解决了， 但这里并没有结束， 我们在这里添加的 ranges 属性的作用是啥呢？为什么要有这个ranges属性？</p>
<h2 id="1-ranges-属性介绍"><a href="#1-ranges-属性介绍" class="headerlink" title="1. ranges 属性介绍  "></a><font size=3>1. ranges 属性介绍  </font></h2><p>ranges 属性是一种用于描述设备之间地址映射关系的属性。 它在设备树（Device Tree） 中使用， 用于描述子设备地址空间如何映射到父设备地址空间。 设备树是一种硬件描述语言， 用于描述嵌入式系统中的硬件组件和它们之间的连接关系。  </p>
<p>设备树中的每个设备节点都可以具有 ranges 属性， 其中包含了地址映射的信息。 下面是一个常见的格式：  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ranges = &lt;child-bus-address parent-bus-address length&gt;;</span><br><span class="line"><span class="comment">// 或者</span></span><br><span class="line">ranges;</span><br></pre></td></tr></table></figure>

<ul>
<li>child-bus-address： 子设备地址空间的起始地址。 它指定了子设备在父设备地址空间中的位置。 具体的字长由 ranges 所在节点的 #address-cells 属性决定。  </li>
<li>parent-bus-address： 父设备地址空间的起始地址。 它指定了父设备中用于映射子设备的地址范围。 具体的字长由 ranges 的父节点的 #address-cells 属性决定。  </li>
<li>length： 映射的大小。 它指定了子设备地址空间在父设备地址空间中的长度。 具体的字长由 ranges 的父节点的 #size-cells 属性决定。</li>
</ul>
<p>当 ranges 属性的值为空时， 表示子设备地址空间和父设备地址空间具有完全相同的映射，即 1:1 映射。 这通常用于描述内存区域， 其中子设备和父设备具有相同的地址范围。 </p>
<p>当 ranges 属性的值不为空时， 按照指定的映射规则将子设备地址空间映射到父设备地址空间。 具体的映射规则取决于设备树的结构和设备的特定要求。  </p>
<h2 id="2-举个例子"><a href="#2-举个例子" class="headerlink" title="2. 举个例子"></a><font size=3>2. 举个例子</font></h2><h3 id="2-1-实例1"><a href="#2-1-实例1" class="headerlink" title="2.1 实例1"></a><font size=3>2.1 实例1</font></h3><h4 id="2-1-1-设备树实例"><a href="#2-1-1-设备树实例" class="headerlink" title="2.1.1 设备树实例"></a><font size=3>2.1.1 设备树实例</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br></pre></td><td class="code"><pre><span class="line">/ &#123;</span><br><span class="line">    <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">    <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line"></span><br><span class="line">    demo_level0 &#123;</span><br><span class="line">        compatible = <span class="string">&quot;simple-bus&quot;</span>;</span><br><span class="line">        ranges = &lt;<span class="number">0x0</span> <span class="number">0x3000000</span> <span class="number">0x3000</span>&gt;;</span><br><span class="line">        <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">        <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line"></span><br><span class="line">        range@<span class="number">0</span> &#123;</span><br><span class="line">            compatible = <span class="string">&quot;range&quot;</span>;</span><br><span class="line">            reg = &lt;<span class="number">0x100</span> <span class="number">0x200</span>&gt;;</span><br><span class="line">            reg-names = <span class="string">&quot;range0&quot;</span>;</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        range@<span class="number">1</span> &#123;</span><br><span class="line">            compatible = <span class="string">&quot;range&quot;</span>;</span><br><span class="line">            reg = &lt;<span class="number">0x300</span> <span class="number">0x200</span>&gt;;</span><br><span class="line">            reg-names = <span class="string">&quot;range1&quot;</span>;</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        range@<span class="number">2</span> &#123;</span><br><span class="line">            compatible = <span class="string">&quot;range&quot;</span>;</span><br><span class="line">            reg = &lt;<span class="number">0x600</span> <span class="number">0x200</span>&gt;;</span><br><span class="line">            reg-names = <span class="string">&quot;range2&quot;</span>;</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        demo_level1 &#123;</span><br><span class="line">            compatible = <span class="string">&quot;simple-bus&quot;</span>;</span><br><span class="line">            ranges = &lt;<span class="number">0x0</span> <span class="number">0x1000</span> <span class="number">0x1000</span>&gt;;</span><br><span class="line">            <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">            <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line"></span><br><span class="line">            range@<span class="number">3</span> &#123;</span><br><span class="line">                compatible = <span class="string">&quot;range&quot;</span>;</span><br><span class="line">                reg = &lt;<span class="number">0x100</span> <span class="number">0x200</span>&gt;;</span><br><span class="line">                reg-names = <span class="string">&quot;range3&quot;</span>;</span><br><span class="line">            &#125;;</span><br><span class="line"></span><br><span class="line">            demo_level1<span class="number">-1</span> &#123;</span><br><span class="line">                compatible = <span class="string">&quot;simple-bus&quot;</span>;</span><br><span class="line">                ranges = &lt;<span class="number">0x0</span> <span class="number">0x300</span> <span class="number">0x500</span>&gt;;</span><br><span class="line">                <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">                <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line"></span><br><span class="line">                range@<span class="number">4</span> &#123;</span><br><span class="line">                    compatible = <span class="string">&quot;range&quot;</span>;</span><br><span class="line">                    reg = &lt;<span class="number">0x100</span> <span class="number">0x200</span>&gt;;</span><br><span class="line">                    reg-names = <span class="string">&quot;range4&quot;</span>;</span><br><span class="line">                &#125;;</span><br><span class="line"></span><br><span class="line">                range@<span class="number">5</span> &#123;</span><br><span class="line">                    compatible = <span class="string">&quot;range&quot;</span>;</span><br><span class="line">                    reg = &lt;<span class="number">0x300</span> <span class="number">0x100</span>&gt;;</span><br><span class="line">                    reg-names = <span class="string">&quot;range5&quot;</span>;</span><br><span class="line">                &#125;;</span><br><span class="line"></span><br><span class="line">                demo_level1<span class="number">-1</span><span class="number">-1</span> &#123;</span><br><span class="line">                    compatible = <span class="string">&quot;simple-bus&quot;</span>;</span><br><span class="line">                    ranges = &lt;<span class="number">0x0</span> <span class="number">0x400</span> <span class="number">0x100</span>&gt;;</span><br><span class="line">                    <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">                    <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line"></span><br><span class="line">                    range@<span class="number">6</span> &#123;</span><br><span class="line">                        compatible = <span class="string">&quot;range&quot;</span>;</span><br><span class="line">                        reg = &lt;<span class="number">0x50</span> <span class="number">0x30</span>&gt;;</span><br><span class="line">                        reg-names = <span class="string">&quot;range6&quot;</span>;</span><br><span class="line">                    &#125;;</span><br><span class="line"></span><br><span class="line">                    demo_level1<span class="number">-1</span><span class="number">-1</span><span class="number">-1</span> &#123;</span><br><span class="line">                        compatible = <span class="string">&quot;simple-bus&quot;</span>;</span><br><span class="line">                        ranges = &lt;<span class="number">0x0</span> <span class="number">0x20</span> <span class="number">0x20</span>&gt;;</span><br><span class="line">                        <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">                        <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line"></span><br><span class="line">                        range@<span class="number">7</span> &#123;</span><br><span class="line">                            compatible = <span class="string">&quot;range&quot;</span>;</span><br><span class="line">                            reg = &lt;<span class="number">0x10</span> <span class="number">0x10</span>&gt;;</span><br><span class="line">                            reg-names = <span class="string">&quot;range7&quot;</span>;</span><br><span class="line">                        &#125;;</span><br><span class="line"></span><br><span class="line">                        range@<span class="number">8</span> &#123;</span><br><span class="line">                            compatible = <span class="string">&quot;range&quot;</span>;</span><br><span class="line">                            reg = &lt;<span class="number">0x0</span> <span class="number">0x10</span>&gt;;</span><br><span class="line">                            reg-names = <span class="string">&quot;range8&quot;</span>;</span><br><span class="line">                        &#125;;</span><br><span class="line">                    &#125;;</span><br><span class="line">                &#125;;</span><br><span class="line">            &#125;;</span><br><span class="line"></span><br><span class="line">            range@<span class="number">9</span> &#123;</span><br><span class="line">                compatible = <span class="string">&quot;range&quot;</span>;</span><br><span class="line">                reg = &lt;<span class="number">0x800</span> <span class="number">0x50</span>&gt;;</span><br><span class="line">                reg-names = <span class="string">&quot;range9&quot;</span>;</span><br><span class="line">            &#125;;</span><br><span class="line"></span><br><span class="line">            demo_level1<span class="number">-2</span> &#123;</span><br><span class="line">                compatible = <span class="string">&quot;simple-bus&quot;</span>;</span><br><span class="line">                ranges = &lt;<span class="number">0x0</span> <span class="number">0x900</span> <span class="number">0x100</span>&gt;;</span><br><span class="line">                <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">                <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line"></span><br><span class="line">                range@<span class="number">10</span> &#123;</span><br><span class="line">                    compatible = <span class="string">&quot;range&quot;</span>;</span><br><span class="line">                    reg = &lt;<span class="number">0x0</span> <span class="number">0x50</span>&gt;;</span><br><span class="line">                    reg-names = <span class="string">&quot;range10&quot;</span>;</span><br><span class="line">                &#125;;</span><br><span class="line"></span><br><span class="line">                demo_level1<span class="number">-2</span><span class="number">-1</span> &#123;</span><br><span class="line">                    compatible = <span class="string">&quot;simple-bus&quot;</span>;</span><br><span class="line">                    ranges;</span><br><span class="line">                    <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">                    <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line"></span><br><span class="line">                    range@<span class="number">11</span> &#123;</span><br><span class="line">                        compatible = <span class="string">&quot;range&quot;</span>;</span><br><span class="line">                        reg = &lt;<span class="number">0x50</span> <span class="number">0x30</span>&gt;;</span><br><span class="line">                        reg-names = <span class="string">&quot;range11&quot;</span>;</span><br><span class="line">                    &#125;;</span><br><span class="line">                &#125;;</span><br><span class="line">            &#125;;</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        demo_level2 &#123;</span><br><span class="line">            compatible = <span class="string">&quot;simple-bus&quot;</span>;</span><br><span class="line">            ranges;</span><br><span class="line">            <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">            <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line"></span><br><span class="line">            range@<span class="number">12</span> &#123;</span><br><span class="line">                compatible = <span class="string">&quot;range&quot;</span>;</span><br><span class="line">                reg = &lt;<span class="number">0x2000</span> <span class="number">0x1000</span>&gt;;</span><br><span class="line">                reg-names = <span class="string">&quot;range12&quot;</span>;</span><br><span class="line">            &#125;;</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h4 id="2-1-2-驱动demo"><a href="#2-1-2-驱动demo" class="headerlink" title="2.1.2 驱动demo"></a><font size=3>2.1.2 驱动demo</font></h4><p>下面是一个简单的驱动，功能很简单，只是在probe函数中将memory资源的start和(end+1)打印出来.</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//demo_range.c:</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/init.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/module.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/platform_device.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;linux/of.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">demo_range_probe</span><span class="params">(<span class="keyword">struct</span> platform_device *pdev)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">resource</span> *<span class="title">res</span> =</span> platform_get_resource(pdev, IORESOURCE_MEM, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">    printk(KERN_INFO <span class="string">&quot;%s start: 0x%x, end: 0x%x\n&quot;</span>,</span><br><span class="line">        res-&gt;name, res-&gt;start, res-&gt;end + <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">demo_range_remove</span><span class="params">(<span class="keyword">struct</span> platform_device *pdev)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">of_device_id</span> <span class="title">demo_range_of_match</span>[]  =</span> &#123;</span><br><span class="line">    &#123; .compatible = <span class="string">&quot;range&quot;</span>&#125;,</span><br><span class="line">    &#123;&#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="class"><span class="keyword">struct</span> <span class="title">platform_driver</span> <span class="title">demo_range_driver</span> =</span> &#123;</span><br><span class="line">    .driver = &#123;</span><br><span class="line">        .name = <span class="string">&quot;demo_range&quot;</span>,</span><br><span class="line">        .owner = THIS_MODULE,</span><br><span class="line">        .of_match_table = demo_range_of_match,</span><br><span class="line">    &#125;,</span><br><span class="line">    .probe = demo_range_probe,</span><br><span class="line">    .remove = demo_range_remove,</span><br><span class="line">&#125;;</span><br><span class="line">module_platform_driver(demo_range_driver);</span><br><span class="line"></span><br><span class="line">MODULE_LICENSE(<span class="string">&quot;GPL v2&quot;</span>);</span><br></pre></td></tr></table></figure>

<p>在驱动中会获得memory资源，然后将start和(end+1)打印出来，之所以这里用(end+1)，仅仅是为了便于理解下面的kernel log。</p>
<h4 id="2-1-3-开发板测试"><a href="#2-1-3-开发板测试" class="headerlink" title="2.1.3 开发板测试"></a><font size=3>2.1.3 开发板测试</font></h4><p>编译驱动，然后加载，可以看到下面的打印信息：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">[root@vexpress mnt]# insmod demo_range.ko </span><br><span class="line">[  382.940402] range0 start: 0x3000100, end: 0x3000300</span><br><span class="line">[  382.940697] range1 start: 0x3000300, end: 0x3000500</span><br><span class="line">[  382.941448] range2 start: 0x3000600, end: 0x3000800</span><br><span class="line">[  382.941657] range3 start: 0x3001100, end: 0x3001300</span><br><span class="line">[  382.941855] range4 start: 0x3001400, end: 0x3001600</span><br><span class="line">[  382.942057] range5 start: 0x3001600, end: 0x3001700</span><br><span class="line">[  382.942262] range6 start: 0x3001750, end: 0x3001780</span><br><span class="line">[  382.942470] range7 start: 0x3001730, end: 0x3001740</span><br><span class="line">[  382.942684] range8 start: 0x3001720, end: 0x3001730</span><br><span class="line">[  382.949796] range9 start: 0x3001800, end: 0x3001850</span><br><span class="line">[  382.950023] range10 start: 0x3001900, end: 0x3001950</span><br><span class="line">[  382.950603] range11 start: 0x3001950, end: 0x3001980</span><br><span class="line">[  382.950805] range12 start: 0x3002000, end: 0x3003000</span><br></pre></td></tr></table></figure>

<p>画成框图帮助理解就是这样的：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-10-%E8%AE%BE%E5%A4%87%E6%A0%91-05-ranges%E5%B1%9E%E6%80%A7/img/480488-20170820191851725-1533846276.png" alt="img"  />

<h4 id="2-1-4-总结"><a href="#2-1-4-总结" class="headerlink" title="2.1.4 总结"></a><font size=3>2.1.4 总结</font></h4><ul>
<li>（1）ranges属性值的格式 &lt;**local地址**  **parent地址**  **size**&gt;， 表示将local地址向parent地址的转换。</li>
</ul>
<p>比如对于#address-cells和#size-cells都为1的话，以&lt;0x0  0x10 0x20&gt;为例，表示将local的从0x0~(0x0 + 0x20)的地址空间映射到parent的0x10~(0x10 + 0x20)</p>
<p>其中，<strong>local地址</strong>的个数取决于当前含有ranges属性的节点的#address-cells属性的值，<strong>size</strong>取决于当前含有ranges属性的节点的#size-cells属性的值。而<strong>parent地址</strong>的个数取决于当前含有ranges属性的节点的parent节点的#address-cells的值。</p>
<ul>
<li>（2）对于含有ranges属性的节点的子节点来说，其reg都是基于<strong>local地址</strong>的</li>
<li>（3）ranges属性值为空的话，表示1:1映射</li>
<li>（4）对于没有ranges属性的节点，代表不是memory map区域。</li>
</ul>
<h3 id="2-2-实例2"><a href="#2-2-实例2" class="headerlink" title="2.2 实例2"></a><font size=3>2.2 实例2</font></h3><p>以下面的设备树为例进行 ranges 属性的学习：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">/dts-v1/;</span><br><span class="line">/ &#123;</span><br><span class="line">    compatible = <span class="string">&quot;acme,coyotes-revenge&quot;</span>;</span><br><span class="line">    <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">    <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">    <span class="comment">//......</span></span><br><span class="line">    external-bus &#123;</span><br><span class="line">        <span class="meta">#address-cells = <span class="string">&lt;2&gt;</span>;</span></span><br><span class="line">        <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">        ranges = &lt;<span class="number">0</span> <span class="number">0</span> <span class="number">0x10100000</span> <span class="number">0x10000</span></span><br><span class="line">                  <span class="number">1</span> <span class="number">0</span> <span class="number">0x10160000</span> <span class="number">0x10000</span></span><br><span class="line">                  <span class="number">2</span> <span class="number">0</span> <span class="number">0x30000000</span> <span class="number">0x30000000</span>&gt;;</span><br><span class="line">        <span class="comment">// Chipselect 1, Ethernet</span></span><br><span class="line">        <span class="comment">// Chipselect 2, i2c controller</span></span><br><span class="line">        <span class="comment">// Chipselect 3, NOR Flash</span></span><br><span class="line">        <span class="comment">//.......</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>在 external-bus 节点中#address-cells 属性值为 2 表示 child-bus-address 由两个值表示， 也就是 0 和 0， 父节点的 #address-cells 属性值和#size-cells 属性值为 1， 表示 parent-bus-address和 length 都由一个表示， 也就是 0x10100000 和 0x10000 ， 该 ranges 值表示将子地址空间（0x0-0xFFFF） 映射到父地址空间 0x10100000 - 0x1010FFFF， 这里的例子为带参数 ranges 属性映射， 不带参数的 ranges 属性为 1： 1 映射， 较为简单。</p>
<h3 id="2-3-总结"><a href="#2-3-总结" class="headerlink" title="2.3 总结"></a><font size=3>2.3 总结</font></h3><p>在嵌入式系统中， 不同的设备可能连接到相同的总线或总线控制器上， 它们需要在物理地址空间中进行正确的映射， 以便进行数据交换和通信。 例如， 一个设备可能通过总线连接到主处理器或其他设备， 而这些设备的物理地址范围可能不同。 <strong>ranges 属性就是用来描述这种地址映射关系的</strong>。  </p>
<h2 id="3-设备分类"><a href="#3-设备分类" class="headerlink" title="3. 设备分类  "></a><font size=3>3. 设备分类  </font></h2><p>根据上面讲解的映射关系可以将设备分为两类： 内存映射型设备和非内存映射型设备。  </p>
<h3 id="3-1-内存映射型设备"><a href="#3-1-内存映射型设备" class="headerlink" title="3.1 内存映射型设备  "></a><font size=3>3.1 内存映射型设备  </font></h3><p>内存映射型设备是指可以通过内存地址进行直接访问的设备。 这类设备在物理地址空间中的一部分被映射到系统的内存地址空间中， 使得 CPU 可以通过读写内存地址的方式与设备进行通信和控制。有以下特点：</p>
<p>（1） 直接访问： 内存映射型设备可以被 CPU 直接访问， 类似于访问内存中的数据。 这种直接访问方式提供了高速的数据传输和低延迟的设备操作。</p>
<p>（2） 内存映射： 设备的寄存器、 缓冲区等资源被映射到系统的内存地址空间中， 使用读写内存的方式与设备进行通信。</p>
<p>（3） 读写操作： CPU 可以通过读取和写入映射的内存地址来与设备进行数据交换和控制操作。</p>
<p>在设备树中， 内存映射型设备的设备树例子如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">/dts-v1/;</span><br><span class="line">/ &#123;</span><br><span class="line">    <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">    <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">    ranges;</span><br><span class="line">    serial@<span class="number">101f</span>0000 &#123;</span><br><span class="line">        compatible = <span class="string">&quot;arm,pl011&quot;</span>;</span><br><span class="line">        reg = &lt;<span class="number">0x101f0000</span> <span class="number">0x1000</span>&gt;;</span><br><span class="line">    &#125;;</span><br><span class="line">    gpio@<span class="number">101f</span>3000 &#123;</span><br><span class="line">        compatible = <span class="string">&quot;arm,pl061&quot;</span>;</span><br><span class="line">        reg = &lt;<span class="number">0x101f3000</span> <span class="number">0x1000</span></span><br><span class="line">        <span class="number">0x101f4000</span> <span class="number">0x10</span>&gt;;</span><br><span class="line">    &#125;;</span><br><span class="line">    spi@<span class="number">10115000</span> &#123;</span><br><span class="line">        compatible = <span class="string">&quot;arm,pl022&quot;</span>;</span><br><span class="line">        reg = &lt;<span class="number">0x10115000</span> <span class="number">0x1000</span>&gt;;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>第 5 行的 ranges 属性表示该设备树中会进行 1： 1 的地址范围映射。  </p>
<h3 id="3-2-非内存映射型设备"><a href="#3-2-非内存映射型设备" class="headerlink" title="3.2 非内存映射型设备  "></a><font size=3>3.2 非内存映射型设备  </font></h3><p>非内存映射型设备是指不能通过内存地址直接访问的设备。 这类设备可能采用其他方式与CPU 进行通信， 例如通过 I&#x2F;O 端口、 专用总线或特定的通信协议。有以下特点：</p>
<p>（1） 非内存访问： 非内存映射型设备不能像内存映射型设备那样直接通过内存地址进行访问。 它们可能使用独立的 I&#x2F;O 端口或专用总线进行通信。</p>
<p>（2） 特定接口： 设备通常使用特定的接口和协议与 CPU 进行通信和控制， 例如 SPI、 I2C、UART 等。</p>
<p>（3） 驱动程序： 非内存映射型设备通常需要特定的设备驱动程序来实现与 CPU 的通信和控制。</p>
<p>在设备树中， 非内存映射型设备的设备树例子如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">/dts-v1/;</span><br><span class="line">/ &#123;</span><br><span class="line">    compatible = <span class="string">&quot;acme,coyotes-revenge&quot;</span>;</span><br><span class="line">    <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">    <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">    <span class="comment">//......</span></span><br><span class="line">    external-bus &#123;</span><br><span class="line">        <span class="meta">#address-cells = <span class="string">&lt;2&gt;</span>;</span></span><br><span class="line">        <span class="meta">#size-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">        ranges = &lt;<span class="number">0</span> <span class="number">0</span> <span class="number">0x10100000</span> <span class="number">0x10000</span></span><br><span class="line">                  <span class="number">1</span> <span class="number">0</span> <span class="number">0x10160000</span> <span class="number">0x10000</span></span><br><span class="line">                  <span class="number">2</span> <span class="number">0</span> <span class="number">0x30000000</span> <span class="number">0x30000000</span>&gt;;</span><br><span class="line">        <span class="comment">// Chipselect 1, Ethernet</span></span><br><span class="line">        <span class="comment">// Chipselect 2, i2c controller</span></span><br><span class="line">        <span class="comment">// Chipselect 3, NOR Flash</span></span><br><span class="line">        ethernet@<span class="number">0</span>,<span class="number">0</span> &#123;</span><br><span class="line">        compatible = <span class="string">&quot;smc,smc91c111&quot;</span>;</span><br><span class="line">            reg = &lt;<span class="number">0</span> <span class="number">0</span> <span class="number">0x1000</span>&gt;;</span><br><span class="line">        &#125;;</span><br><span class="line">        i2c@<span class="number">1</span>,<span class="number">0</span> &#123;</span><br><span class="line">            compatible = <span class="string">&quot;acme,a1234-i2c-bus&quot;</span>;</span><br><span class="line">            <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">            <span class="meta">#size-cells = <span class="string">&lt;0&gt;</span>;</span></span><br><span class="line">            reg = &lt;<span class="number">1</span> <span class="number">0</span> <span class="number">0x1000</span>&gt;;</span><br><span class="line">            rtc@<span class="number">58</span> &#123;</span><br><span class="line">                compatible = <span class="string">&quot;maxim,ds1338&quot;</span>;</span><br><span class="line">                reg = &lt;<span class="number">0x58</span>&gt;;</span><br><span class="line">            &#125;;</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="4-映射地址计算"><a href="#4-映射地址计算" class="headerlink" title="4. 映射地址计算  "></a><font size=3>4. 映射地址计算  </font></h2><p>接下来以上面列举的非内存映射型设备的设备树中的 ethernet@0 节点为例， 计算该网卡设备的映射地址。  </p>
<p>首先， 找到 ethernet@0 所在的节点， 并查看其 reg 属性。 在给定的设备树片段中， ethernet@0 的 reg 属性为&lt;0 0 0x1000&gt;。 在根节点中， #address-cells 的值为 1， 表示地址由一个单元格组成。  </p>
<p>接下来， 根据 ranges 属性进行地址映射计算。 在 external-bus 节点的 ranges 属性中， 有三个映射条目：  </p>
<p>第一个映射条目为“0 0 0x10100000 0x10000” ， 表示外部总线的地址范围为 0x10100000到 0x1010FFFF。 该映射条目的第一个值为 0， 表示与 external-bus 节点的第一个子节点（ethernet@0,0） 相关联。  </p>
<p>第二个映射条目： “1 0 0x10160000 0x10000” ， 表示外部总线的地址范围为 0x10160000到 0x1016FFFF。该映射条目的第一个值为 1， 表示与 external-bus 节点的第二个子节点（i2c@1,0）相关联。  </p>
<p>第三个映射条目：“2 0 0x30000000 0x30000000”， 表示外部总线的地址范围为 0x30000000 到 0x5FFFFFFF。 该映射条目的第一个值为 2， 表示与 external-bus 节点的第三个子节点相关联。   </p>
<p>由于ethernet@0与external-bus的第一个子节点相关联， 并且它的reg属性为&lt;0 0 0x1000&gt;，我们可以进行以下计算：  </p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ethernet@0 的物理起始地址 = 外部总线地址起始值=0x10100000</span><br><span class="line">ethernet@0 的物理结束地址 = 外部总线地址起始值 + （ethernet@0 的 reg 属性的第二个值-1）</span><br><span class="line">                       = 0x10100000 + 0xFFF</span><br><span class="line">                       = 0x10100FFF</span><br></pre></td></tr></table></figure>

<p>因此， ethernet@0 的物理地址范围为 0x10100000 - 0x10100FFF。</p>
<blockquote>
<p>参考资料</p>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/pengdonglin137/p/7401049.html">设备树中ranges属性分析（1） - dolinux - 博客园</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/238c1e95.html">LV06-10-设备树-05-ranges属性</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2025年03月17日 - 07:45</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/238c1e95.html" title="LV06-10-设备树-05-ranges属性">https://sumumm.github.io/post/238c1e95.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> LV06-驱动开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/4c056b.html" rel="prev" title="LV06-11-设备树插件-01-设备树插件基础">
                  <i class="fa fa-angle-left"></i> LV06-11-设备树插件-01-设备树插件基础
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/3df14f88.html" rel="next" title="LV06-10-设备树-04-设备树的应用">
                  LV06-10-设备树-04-设备树的应用 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
